home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-01
/
lcppb.zip
/
LCPP04.ZIP
/
POPUP.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1991-07-03
|
4KB
|
141 lines
// popup.cpp -- Display overlapping "pop-up" windows
#include "popup.h"
main()
{
int wnum;
unsigned trow, lcol, brow, rcol; // Window coordinates
winrec buffer[MAXWINDOW]; // Array of winrecs
srand(time(NULL)); // Randomize
disp_open(); // Initialize display package
/* -- Display MAXWINDOW windows at random locations and colors */
for (wnum = 0; wnum < MAXWINDOW; wnum++) {
trow = randrange(0, MAXROW / 2); // Set top row
lcol = randrange(0, MAXCOL / 2); // Set left column
brow = randrange(trow + 4, MAXROW); // Set bottom row
rcol = randrange(lcol + 12, MAXCOL); // Set right column
openWindow(
&buffer[wnum], // Pass winrec pointer
randrange(1, MAXCOLOR), // Background color
trow, lcol, brow, rcol); // Location
// displayText(&buffer[wnum], form("Window #%d", wnum + 1));
char string[20];
sprintf(string, "Window #%d", wnum + 1);
displayText(&buffer[wnum], string);
}
/* -- Close windows one by one */
disp_move(24, 0);
disp_printf("Press <Space> to erase top window...");
for (wnum = MAXWINDOW - 1; wnum >= 0; wnum--)
closeTopWindow(&buffer[wnum]);
disp_move(24, 0); // Move cursor to last row
disp_close(); // Close display package
}
/* -- Display error message and quit */
void error(char *errmsg) {
disp_move(24, 0); // Move cursor to last row
disp_close(); // Close the display package
cout << "ERROR: " << errmsg; // Display error message
exit(1); // Return to DOS
}
/* -- Pause for a keypress (no message displayed) */
void pause(void)
{
/*int c =*/ getch(); // Wait for any keypress
}
/* -- Return random number from low ... high */
int randrange(int low, int high)
{
return low + (rand() % ((high - low) + 1));
}
/* -- Open a new window at these coordinates and fill with color.
Background color is ignored on monochrome displays. */
void openWindow(winrecptr wrp, int backcolor, unsigned trow,
unsigned lcol, unsigned brow, unsigned rcol)
{
int bcolor, tcolor; // Border color, text color
unsigned bufsize; // Saved-text buffer size
/* -- Determine color to use for background and foreground */
if (disp_getmode() == MONOCHROME) {
bcolor = DISP_REVERSEVIDEO; // Monochrome display
tcolor = bcolor;
} else {
bcolor = (backcolor << 4) + WHITE; // Color display
tcolor = bcolor - WHITE;
}
/* -- Calculate number of bytes needed to save text behind window */
bufsize =
((brow - trow + 1) * (rcol - lcol + 1)) * sizeof(unsigned);
wrp->bufptr = malloc(bufsize); // Reserve space for buffer
if (wrp->bufptr == NULL) // If that fails, return
return; // leaving bufptr == NULL
/* -- Save text behind window in buffer just created */
disp_peekbox(wrp->bufptr, trow, lcol, brow, rcol);
wrp->trow = trow; // Save coordinates and color, too
wrp->lcol = lcol;
wrp->brow = brow;
wrp->rcol = rcol;
wrp->attribute = tcolor; // Text color
/* -- Pause and then display the window */
// msleep(250); // Pause for 1/4 second
delay(250); // Pause for 1/4 second
disp_fillbox(((bcolor * 256) + ' '), trow, lcol, brow, rcol);
disp_box(0, bcolor, trow, lcol, brow, rcol);
}
/* -- Display some text inside this window (assumes that window is
frontmost on display) */
void displayText(winrecptr wrp, char *message)
{
if (wrp->bufptr == NULL) return; // Exit if window not open
disp_move(wrp->trow + 2, wrp->lcol + 2);
disp_setattr(wrp->attribute);
disp_printf(message);
disp_setattr(DISP_NORMAL);
}
/* -- Close window using winrec struct addressed by wrp. Assumes that
this window is frontmost on display. */
void closeTopWindow(winrecptr wrp)
{
if (wrp->bufptr == NULL) return; // Exit if window not open
pause();
disp_pokebox(
wrp->bufptr, wrp->trow, wrp->lcol, wrp->brow, wrp->rcol);
free(wrp->bufptr); // Dispose buffer
wrp->bufptr = NULL; // Prevent further use of buffer
}
// Copyright (c) 1990 by Tom Swan. All rights reserved
// Revision 1.00 Date: 08/30/1990 Time: 09:40 pm
// Revision 1.01 Date: 07/03/1991 Time: 04:00 pm
// Converted for Borland C++ 2.0